import VueRouter from 'vue-router'
import Vue from 'vue'
import Home from '../pages/Index.vue'
import Login from '../pages/Login.vue'
import Label from '../pages/Label.vue'
import Product from '../pages/Product.vue'
import User from '../pages/User.vue'
import SearchResult from '../pages/SearchResult.vue'

Vue.use(VueRouter)

function getCookie(cookieName) {
	var strCookie = document.cookie;
	var arrCookie = strCookie.split("; ");
	for (var i = 0; i < arrCookie.length; i++) {
		var arr = arrCookie[i].split("=");
		if (cookieName == arr[0]) {
			return arr[1];
		}
	}
	return "";
}


const router = new VueRouter({
	mode: 'history',
	routes: [
		{
			path:'/',
			redirect: '/login'
		},
		{
			path: '/login',
			name: 'login',
			component: Login,
			meta: {
				title: '海燕产品管理系统-登录',
			}
		},
		{
			path: '/index',
			name: '项目管理',
			component: Home,
			meta: {
				title: '海燕产品管理系统-项目管理',
			}
		},
		{
			path: '/label',
			name: '字段管理',
			component: Label,
			meta: {
				title: '海燕产品管理系统-字段管理',
			}
		},
		{
			path: '/product',
			name: '全部产品',
			component: Product,
			meta: {
				title: '海燕产品管理系统-全部产品',
			}
		},
		{
			path:'/user',
			name:'账号管理',
			component:User,
			meta:{
				title:'海燕产品管理系统-账号管理'
			}
		},
		{
			path: '/search',
			name: 'search',
			component: SearchResult,
			meta: {
				title: '海燕接线盒查询系统',
			}
		}
	]
})
router.beforeEach((to, from, next) => {
	let islogin = getCookie('loginstatus');
	islogin = +islogin;
	if(to.meta.title){
		document.title = to.meta.title
	}
	if(islogin){
		if(to.name == 'login'){
			next('/index')
		}else{
			next()
		}
	}else{
		if(to.name =='login' || to.name == 'search'){
			next()
		}else{
			next('/login')
		}
	}
})


export default router
